/**
 * Created by yaohuan on 2018/4/17 0017.
 */
define(function () {
    // 全局遮罩
    var mask;
    var self, pjjsData;
    var list = [];

    function pjjsinfo() {
        self = this;
        self.productData = {};
        self.mData = {
            option:[],//选择客户和维修单号
            imgs: [],
            form: {

                MMRU007: app.cookie('_WX_MOBILE_'),
                MMRU008: '',//寄件地址
                MMRU009: '',//收件人名称
                MMRU010: '',//收件人电话
                MMRU011: '',//收件人地址
                MMRU012: '',//承运方名称
                MMRU013: '',//承运方电话
                MMRU014: '',//照片地址
                MMRU015: '',//维修单号
                MMRU016: '',//服务客户
                MMRU017: '',//备注
                MMRU101: '',//快递单号
                MMRUDOCDATE: '',
            }
        }
        self.init();
    }

    //数据获取
    pjjsinfo.prototype.getData = function () {
        self.mData.option.splice(0,self.mData.option.length);

        app.ajax({
            xurl: 'api/wx/pjjs/getMmrtData',
            xdata: {
                page: 1,
                limit: 10,
                start: 0
            },
            cb: function (result) {
                if (result.ok) {
                    var data_ = result.data;
                    self.mData.option.pushArray(data_.data);
                }
            }
        })
    };



    //获取配件列表
    pjjsinfo.prototype.init = function () {

        pjjsData = app.pageParameters['pjjsinfo_page'];
        mask = mui.createMask();
        mui.ready(function () {

            Vue.component('option-listitem', {
                template: app.getByid(doc, 'optionitem-template'),
                props: {
                    model: Array,
                },
                methods: {
                    select:function (v) {
                        self.mData.form.MMRU015=v.MMRT012;
                        self.mData.form.MMRU016=v.MMRT013;
                        mask.close();
                        $(".modal1").css("display", "none");
                    }
                }
            });

            //寄送信息
            new Vue({
                el: document.getElementById("spare_send"),
                data: {
                    md: self.mData
                },
                methods: {
                    img_add: function () {
                        app.chooseImageNew(function (imgs) {
                            //self.mData.imgs.pushArray(imgs);
                            for (var i = 0; i < imgs.length; i++) {
                                self.mData.imgs.push({
                                    img: imgs[i].img,
                                    id: imgs[i].localId
                                })
                            }
                        });
                    },
                    submit: function () { //保存
                        app.confirm({
                            msg: '是否提交？',
                            ok: function (e) {
                            //app.showLoading();
                            app.uploadLocalIds({
                            ids: self.mData.imgs,
                            cb: function (imgs) {
                                var imgurl = "";
                                for (var i = 0; i < imgs.length; i++) {
                                    imgurl += (imgs[i].name + ",");
                                }
                                self.mData.form.MMRU004=imgurl.substring(0,imgurl.length-1);
                                //self.mData.form.MMRU014 = imgs.join(',');
                                for (var i = 0; i < pjjsData.items.length; i++) {
                                    if (pjjsData.items[i].checked) {
                                        // pjjsData.items[i].checked = false;

                                        pjjsData.items[i].MMRU007 = self.mData.form.MMRU007;
                                        pjjsData.items[i].MMRU008 = self.mData.form.MMRU008; //寄送地址
                                        pjjsData.items[i].MMRU009 = self.mData.form.MMRU009;
                                        pjjsData.items[i].MMRU010 = self.mData.form.MMRU010;
                                        pjjsData.items[i].MMRU011 = self.mData.form.MMRU011;
                                        pjjsData.items[i].MMRU012 = self.mData.form.MMRU012;//承运方名称
                                        pjjsData.items[i].MMRU013 = self.mData.form.MMRU013;//承运方电话
                                        pjjsData.items[i].MMRU014 = self.mData.form.MMRU014;
                                        pjjsData.items[i].MMRU015 = self.mData.form.MMRU015;
                                        pjjsData.items[i].MMRU016 = self.mData.form.MMRU016;
                                        pjjsData.items[i].MMRU017 = self.mData.form.MMRU017;//备注
                                        pjjsData.items[i].MMRU101 = self.mData.form.MMRU101;
                                        pjjsData.items[i].MMRUDOCDATE = self.mData.form.MMRUDOCDATE;
                                        list.push(pjjsData.items[i]);
                                    }

                                }
                                var _xdata = {};
                                _xdata.list = JSON.stringify(list);

                                app.ajax({
                                    xurl: "api/wx/pjjs/savePjjs",
                                    xdata: _xdata,
                                    cb: function (result) {
                                        if (result.ok) {
                                            app.toast('配件寄送提交成功');
                                            for (var i = 0, flag = true; i < pjjsData.items.length; flag ? i++ : i) {
                                                if (pjjsData.items[i].checked == true) {
                                                    pjjsData.items.splice(i, 1);
                                                    flag = false;
                                                } else {
                                                    flag = true;
                                                }
                                            }
                                            list.splice(0,list.length);
                                            app.back(2);
                                        } else {
                                            app.toast(result.data.message);
                                        }
                                    }
                                });
                            }
                        });
                            }
                        })
                    },
                    cancel: function () {
                        app.back(1);
                    },
                    option:function () {
                        self.getData();
                        $(".modal1").css("display", "block");
                        mask.show();
                    }
                }
            });

            // 开窗
            new Vue({
                el: doc.getElementById('option-modal'),
                data: {
                    items: self.mData.option
                },
                methods:{
                    close:function () {
                        mask.close();
                        $(".modal1").css("display", "none");
                    }
                }
            });
            $('.mui-backdrop').click(function (e) {
            })
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
            });


        })

    }

    return new pjjsinfo();
});